package com.yili99;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.yili99.services.FileServices;
import com.yili99.springmvc.crud.dao.DepartmentDao;
import com.yili99.springmvc.crud.dao.EmployeeDao;
import com.yili99.springmvc.crud.dao.SanguisIndexDao;
import com.yili99.springmvc.crud.entities.Employee;
import com.yili99.springmvc.crud.entities.SanguisIndex2;

@Controller
public class EmployeeHandler {

	@Autowired
	private EmployeeDao employeeDao;

	@Autowired
	private DepartmentDao departmentDao;
	
	@Autowired
	private FileServices fileServices;
	
	@Autowired
	private SanguisIndexDao sanguisIndexDao;

	@RequestMapping("/hello")
	public String hello() {
		System.out.println("hello");
		return "success";
	}

	@RequestMapping("/emps")
	public String list(Map<String, Object> map) {
		System.out.println("list---");
		System.out.println(sanguisIndexDao.getAll());
		map.put("employees", sanguisIndexDao.getAll());
		return "list";
	}

	@RequestMapping("/emp")
	public String input(Map<String, Object> map) {
		Employee employees = new Employee();
		map.put("departments", departmentDao.getDepartments());
		map.put("employee", employees);
		return "input";
	}

	@RequestMapping(value = "/emp", method = RequestMethod.POST)
	public String save(HttpSession session, @RequestParam("file") MultipartFile file,@Valid Employee e, Errors result, Map<String, Object> map) throws IOException {
		System.out.println("save: " + e);
		
		ServletContext context = session.getServletContext();
		InputStream is = file.getInputStream();
        String fileName = file.getOriginalFilename();
        boolean isSaveSuccess = fileServices.saveFile(context, is, fileName);
        
		if (result.getErrorCount() > 0||!isSaveSuccess) {
			System.out.println("输入有误，请重新输入");
			map.put("departments", departmentDao.getDepartments());
			return "input";
		}
        e.setProtrait(fileName);
		employeeDao.save(e);
		return "redirect:/emps";

	}

	@RequestMapping(value = "/emp/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable("id") Integer id) {
		employeeDao.delete(id);
		return "redirect:/emps";
	}

	@RequestMapping(value = "/emp", method = RequestMethod.PUT)
	public String update(Employee employee) {
		employeeDao.equals(employee);
		return "redirect:/emps";
	}

	@RequestMapping(value = "/emp/{id}", method = RequestMethod.GET)
	public String input(@PathVariable("id") Integer id, Map<String, Object> map) {
		map.put("employee", employeeDao.get(id));
		map.put("departments", departmentDao.getDepartments());
		return "input";
	}

	@RequestMapping(value = "/i18n")
	public String i18n() {
		return "redirect:/list";
	}

//	@RequestMapping("/uploadImage")
//	public String uploadPortrait(HttpSession session, @RequestParam("file") MultipartFile file) throws IOException {
//        
//		ServletContext context = session.getServletContext();
//		InputStream is = file.getInputStream();
//        String fileName = file.getOriginalFilename();
//        fileServices.saveFile(context, is, fileName);
//		return "redirect:/list";
//	}

}
